package com.asset.modules.dashboard.controller;


import com.alibaba.fastjson.JSONObject;
import com.asset.modules.assets.model.Asset;
import com.asset.modules.assets.service.AssetService;
import com.asset.modules.budget.model.Budget;
import com.asset.modules.budget.service.BudgetService;
import com.asset.modules.common.model.ResponseBean;
import com.asset.modules.common.utils.DateUtils;
import com.asset.modules.system.service.CompanyService;
import com.asset.modules.system.service.ProjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author chenchen
 */
@RestController
@CrossOrigin
@RequestMapping("/dashboard")
@Api(tags = "可视化接口")
public class DashboardController {
    private static final Logger logger = LoggerFactory.getLogger(DashboardController.class);

    @Autowired
    CompanyService cs;

    @Autowired
    ProjectService ps;

    @Autowired
    BudgetService bs;

    @Autowired
    AssetService as;


    @ApiOperation(value = "根据区域编码与年份查询固资信息，只提供当年数据")
    @RequestMapping(value = "/assetInfo", method = RequestMethod.GET)
    public ResponseBean getAssetInfoByCompanyId(@RequestParam String companyId, @RequestParam String year ){
        QueryWrapper<Asset> qw  = new QueryWrapper<>();
        qw.eq("company_id", companyId);
        List<Asset> list = as.list(qw);
        JSONObject result = new JSONObject();
        long unCheckedNum = 0;
        long checkedNum = 0;
        long discardNum = 0;
        double newAssetValue = 0;
        JSONObject values = new JSONObject();
        String currentYear = DateUtils.formatDateToString(new Date(), DateUtils.DATE_FORMAT_Y);
        if(list.size()>0){
            for(Asset a : list){
                //如果不是当年的低值易耗品，则不参与统计
                System.out.println(a.getBuyTime().substring(0,4));
                if(a.getBuyTime().substring(0, 4).equals(currentYear)&&a.getRegisterMoney()<=2000){
                    continue;
                }
                if(a.getStatus().equals("0")){
                    //未盘点固资数量
                    unCheckedNum +=1;
                }else if(a.getStatus().equals("1")){
                    checkedNum +=1;
                }else if(a.getStatus().equals("-1")){
                    discardNum +=1;
                }

                //新购固资总额
                //TODO 低值易耗品是否纳入统计？ 当年的纳入统计
                if(a.getBuyTime().substring(0,4).equals(year)&&!a.getStatus().equals("-2")){
                    newAssetValue += a.getRegisterMoney();
                }

                //公司固资年度净值
                //TODO 低值易耗品是否纳入统计？
                String key = a.getProjectId()+"-"+a.getProjectName();
                if(values.containsKey(key)){
                    double v = (double)values.get(key);
                    values.put(key, v+a.getValue());
                }else{
                    values.put(key, a.getValue());
                }
            }
        }
        result.put("unCheckedNum", unCheckedNum);
        result.put("checkedNum", checkedNum);
        result.put("discardNum", discardNum);
        result.put("newAssetValue", newAssetValue);
        result.put("values", values);
        return new ResponseBean(HttpStatus.OK.value(), "请求成功！", result);
    }



    @ApiOperation(value = "根据区域编码与年份查询预算信息,只提供当年数据")
    @RequestMapping(value = "/budgetInfo", method = RequestMethod.GET)
    public ResponseBean getBudgetInfoByCompanyId(@RequestParam String companyId, @RequestParam String year){
        QueryWrapper<Budget> qw = new QueryWrapper<>();
        qw.eq("company_id", companyId);
        //查询某年的数据
        qw.eq("substr(create_time, 1, 4)", year);
        List<Budget> list = bs.getListBudget(qw);
        JSONObject result = new JSONObject();
        JSONObject jo = new JSONObject();
        double totalBudget = 0;
        double totalBalance = 0;
        //平台预算
        double totalPlatformBudget = 0;
        //项目部预算
        double totalMngBudget  = 0;
        //营销中心预算
        double totalSaleBudget = 0;
        //房修中心预算
        double totalRepairBudget = 0;

        if(list.size()>0){
            for(Budget b : list){
                //区域总预算
                if(b.getType().equals("2")){
                    totalBudget = Double.valueOf(b.getCompanyBudget());
                }else {
                    //预算余额
                    totalBalance+= b.getBudgetBalance();
                    //平台预算
                    totalPlatformBudget += b.getPlatformBudget();
                    //项目部预算
                    totalMngBudget += b.getMngBudget();
                    //营销中心预算
                    totalSaleBudget += b.getSaleBudget();
                    //房修中心预算
                    totalRepairBudget += b.getRepairBudget();
                    String key  = b.getProjectId() +"-"+b.getProjectName();
                    //各个公司的年度预算统计
                    if(jo.containsKey(key)){
                        double budget = (double)jo.get(key);
                        jo.put(key, budget + b.getProjectBudget());
                    }else{
                        jo.put(key, b.getProjectBudget());
                    }
                }
            }
        }
        result.put("totalBudget", totalBudget);
        result.put("totalBalance", totalBalance);
        result.put("platformBudget", totalPlatformBudget);
        result.put("mngBudget", totalMngBudget);
        result.put("saleBudget", totalSaleBudget);
        result.put("repairBudget", totalRepairBudget);
        result.put("yearlyBudget", jo);
        return new ResponseBean(HttpStatus.OK.value(), "请求成功！", result);
    }


}
